import json
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt


def plot_delay(json_list):
    average_interval = np.zeros(2)
    # 读取第一个json
    json_tmp_1 = json_list[0]
    with open('./' + json_tmp_1, 'r') as load_file:
        http_json_1 = json.load(load_file)
    json_len_1 = len(http_json_1)
    count = np.zeros(6)
    quality_0 = []
    quality_1 = []
    quality_2 = []
    quality_3 = []
    quality_4 = []
    quality_5 = []

    interval_0 = []
    interval_1 = []
    interval_2 = []
    interval_3 = []
    interval_4 = []
    interval_5 = []

    for i in range(json_len_1 - 2):
        face = http_json_1[i]['url'].split('/')[4].strip('face')
        average_interval[0] += http_json_1[i]["interval"]
        if face == '0':
            quality_0.append(http_json_1[i]["_quality"])
            interval_0.append(http_json_1[i]["interval"])
        elif face == '1':
            quality_1.append(http_json_1[i]["_quality"])
            interval_1.append(http_json_1[i]["interval"])
        elif face == '2':
            quality_2.append(http_json_1[i]["_quality"])
            interval_2.append(http_json_1[i]["interval"])
        elif face == '3':
            quality_3.append(http_json_1[i]["_quality"])
            interval_3.append(http_json_1[i]["interval"])
        elif face == '4':
            quality_4.append(http_json_1[i]["_quality"])
            interval_4.append(http_json_1[i]["interval"])
        elif face == '5':
            quality_5.append(http_json_1[i]["_quality"])
            interval_5.append(http_json_1[i]["interval"])
        count[int(face)] += 1

    # 读取第二个json
    with open('./' + json_list[1], 'r') as load_file:
        http_json_2 = json.load(load_file)
    json_len_2 = len(http_json_2)
    count2 = np.zeros(6)
    quality_NoABR_0 = []
    quality_NoABR_1 = []
    quality_NoABR_2 = []
    quality_NoABR_3 = []
    quality_NoABR_4 = []
    quality_NoABR_5 = []

    interval_NoABR_0 = []
    interval_NoABR_1 = []
    interval_NoABR_2 = []
    interval_NoABR_3 = []
    interval_NoABR_4 = []
    interval_NoABR_5 = []

    for i in range(json_len_2 - 2):
        face = http_json_2[i]['url'].split('/')[4].strip('face')
        average_interval[1] += http_json_2[i]["interval"]
        if face == '0':
            quality_NoABR_0.append(http_json_2[i]["_quality"])
            interval_NoABR_0.append(http_json_2[i]["interval"])
        elif face == '1':
            quality_NoABR_1.append(http_json_2[i]["_quality"])
            interval_NoABR_1.append(http_json_2[i]["interval"])
        elif face == '2':
            quality_NoABR_2.append(http_json_2[i]["_quality"])
            interval_NoABR_2.append(http_json_2[i]["interval"])
        elif face == '3':
            quality_NoABR_3.append(http_json_2[i]["_quality"])
            interval_NoABR_3.append(http_json_2[i]["interval"])
        elif face == '4':
            quality_NoABR_4.append(http_json_2[i]["_quality"])
            interval_NoABR_4.append(http_json_2[i]["interval"])
        elif face == '5':
            quality_NoABR_5.append(http_json_2[i]["_quality"])
            interval_NoABR_5.append(http_json_2[i]["interval"])
        count2[int(face)] += 1

    # plt.figure()
    # fig1, ax1 = plt.subplots(figsize=(8, 6))
    # ax1.plot(range(int(count[0])), quality_0, label='face_0')
    # ax1.plot(range(int(count[1])), quality_1, label='face_1')
    # ax1.plot(range(int(count[2])), quality_2, label='face_2')
    # ax1.plot(range(int(count[3])), quality_3, label='face_3')
    # ax1.plot(range(int(count[4])), quality_4, label='face_4')
    # ax1.plot(range(int(count[5])), quality_5, label='face_5')
    # ax1.set_xlabel('Request count')
    # ax1.set_ylabel('Quality')
    # # ax1.axis([-(json_len_1 - 2) * 0.05, (json_len_1 - 2) * 1.05, -0.1, np.max(quality_1)*1.1])
    # ax1.spines["top"].set_color("none")
    # ax1.spines["right"].set_color("none")
    # fig1.legend()
    # fig1.show()
    average_interval[0] /= np.sum(count)
    average_interval[1] /= np.sum(count2)

    fig1, ax1 = plt.subplots(figsize=(8, 6))
    ax1.plot(range(int(count[0])), interval_0, label='face_0')
    ax1.plot(range(int(count[1])), interval_1, label='face_1')
    ax1.plot(range(int(count[2])), interval_2, label='face_2')
    ax1.plot(range(int(count[3])), interval_3, label='face_3')
    ax1.plot(range(int(count[4])), interval_4, label='face_4')
    ax1.plot(range(int(count[5])), interval_5, label='face_5')
    ax1.set_xlabel('Request count', fontsize=12)
    ax1.set_ylabel('Delay without ABR ', fontsize=12)
    ax1.set_title('Average Delay = ' + "{:.2f}".format(average_interval[0]) + 'ms', fontsize=15)
    # ax1.axis([-(json_len_1 - 2) * 0.05, (json_len_1 - 2) * 1.05, -0.1, np.max(quality_1)*1.1])
    ax1.spines["top"].set_color("none")
    ax1.spines["right"].set_color("none")
    fig1.legend()
    fig1.savefig('./delay_with_ABR.png')
    
    # plt.figure()
    fig2, ax2 = plt.subplots(figsize=(8, 6))
    ax2.plot(range(int(count2[0])), interval_NoABR_0, label='face_0')
    ax2.plot(range(int(count2[1])), interval_NoABR_1, label='face_1')
    ax2.plot(range(int(count2[2])), interval_NoABR_2, label='face_2')
    ax2.plot(range(int(count2[3])), interval_NoABR_3, label='face_3')
    ax2.plot(range(int(count2[4])), interval_NoABR_4, label='face_4')
    ax2.plot(range(int(count2[5])), interval_NoABR_5, label='face_5')
    ax2.set_xlabel('Request count', fontsize=12)
    ax2.set_ylabel('Delay with ABR', fontsize=12)
    ax2.set_title('Average Delay = ' + "{:.2f}".format(average_interval[1]) + 'ms', fontsize=15)
    # ax2.axis([-(json_len_1 - 2) * 0.05, (json_len_1 - 2) * 1.05, -0.1, np.max(quality_1)*1.1])
    ax2.spines["top"].set_color("none")
    ax2.spines["right"].set_color("none")
    fig2.legend()
    fig2.savefig('./delay_without_ABR.png')

    fig3, ax3 = plt.subplots(figsize=(3, 4))
    ax3.plot()


if __name__ == '__main__':
    json_list = ['BOLA_httpRequest.json', 'Second_httpRequest.json']
    plot_delay(json_list)
